package com.xaut.outpatientsystem.medicinalManagement.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xaut.outpatientsystem.medicinalManagement.pojo.Medicine;
import com.xaut.outpatientsystem.medicinalManagement.service.MedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/medicine")
public class MedicineController {

    @Autowired
    private MedicineService medicineService;

    @GetMapping("/findById")
    public Medicine getMedicineById(int id) {
        return medicineService.getById(id);
    }

    @GetMapping("/findMedicine")
    public List<Medicine> getAllMedicine() {
        return medicineService.list();
    }

    @PostMapping("/findMedicine")
    public IPage<Medicine> getAllMedicine(@RequestBody Map<String, Object> searchMap, int page, int size) {
        if (searchMap.get("medicine_name") == null || searchMap.get("medicine_name").equals("")) {
            Page<Medicine> page1 = new Page<>(page, size);
            IPage<Medicine> iPage1  = medicineService.page(page1);
            return iPage1;
        }
        System.out.println(searchMap.get("medicine_name"));
        QueryWrapper<Medicine> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("medicine_name", searchMap.get("medicine_name"));
        Page<Medicine> pages = new Page<Medicine>(page, size);
        IPage<Medicine> iPage = medicineService.page(pages, queryWrapper);
        System.out.println("总页数:"+iPage.getPages());
        System.out.println("总记录数:"+iPage.getTotal());
        return iPage;
    }

    @PostMapping("/saveOrUpdate")
    public void saveOrUpdateMedicine(@RequestBody Medicine medicine) {
        System.out.println(medicine);
        medicineService.saveOrUpdate(medicine);
    }

    @GetMapping("/delete")
    public void deleteMedicine(int id) {
        medicineService.removeById(id);
    }

}
